<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" :labelWidth="190" />
    <FilterTable ref="table" :columns="columns" :dataSource="tableList" :isSelectColumn="false" height="auto" :onColumnsChange="columns => (this.columns = columns)">
      <template slot="controls" slot-scope="props"> </template>
    </FilterTable>
    <BreakSpace label="操作记录" />
    <FilterTable ref="tables" :columns="columns1" :dataSource="tableLists" :datakey="'records'" :isSelectColumn="false" height="auto" :onColumnsChange="columns => (this.columns1 = columns)">
    </FilterTable>
    <BaseDialog :visible.sync="isaddCar" :title="'新增车辆'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <selectCar :carData="carData" @close="carcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="iscontract" :title="'选择合同'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <contract :rowData="formData" @close="contractClose" />
    </BaseDialog>
    <BaseDialog :visible.sync="isProject" :title="'选择项目'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AccountProject :rowData="formData" @close="ProjectClose" />
    </BaseDialog>
    <BaseDialog :visible.sync="isDiscount" :title="'折扣详情'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DiscountDetail :rowData="DiscountData" @close="Discountclose" />
    </BaseDialog>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import { getLog } from '@/api/dealerSale/OrderManagement/OrderSubmission/index.js';
import { getDetail, getAmount, getDiscount, getDiscountDetails, saveOrder, getNotPage, getDealerAddress, gettmFiAccountType } from '@/api/omSale/KeyCustomers/OrderSubmission/index.js';
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import DiscountDetail from './DiscountDetail';
import AccountProject from './AccountProject';
import contract from './contract';
import selectCar from './selectCar';
import ownerSeacher from './ownerSeacher';
import { getAppId } from '@/assets/js/auth'; //获取登录用户

import Cookies from 'js-cookie';
export default {
  data() {
    this.saveData = null;
    this.Amount = null;
    this.singleRebateTotalAmount = null;
    this.buildRebateTotalAmount = null;
    return {
      formList: this.createdFrom(),
      columns: this.createdTable(),
      tableList: [],
      isaddCar: false,
      formData: {},
      carData: {},
      json: {},
      isDiscount: false,
      DiscountData: {},
      TotalAmount: '',
      singleRebateAmount: '',
      buildRebateAmount: '',
      isowner: false,
      iscontract: false,
      isProject: false,
      contractId: '',
      dealerCode: '',
      ProjectId: '',
      columns1: this.createdTables(),
      tableLists: []
    };
  },
  components: {
    DiscountDetail,
    AccountProject,
    contract,
    selectCar,
    ownerSeacher
  },
  mounted() {
    this.table = this.$refs.table.$refs.pageTable;
    if (getAppId() != 'beijing') {
      this.formList.find(x => x.fieldName == 'remarks').hidden = true;
    }
  },
  props: ['rowData'],
  created() {
    this.getLog(this.rowData.orderId);
    this.getDetail(this.rowData);
  },
  mixins: [dictionary],
  methods: {
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '大客户订单信息'
        },
        // {
        //   type: 'INPUT',
        //   label: '经销商名称',
        //   fieldName: 'dealerName',
        //   placeholder: '请选择',
        //   disabled: true,
        //   unitRender: () => {
        //     return (
        //       <div>
        //         <el-button icon="el-icon-search" onClick={() => this.seacherFun(3)}></el-button>
        //       </div>
        //     );
        //   },
        //   change: p => {
        //     if (!p) {
        //       this.dealerCode = '';
        //     }
        //   },
        //   rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        // },
        {
          type: 'INPUT',
          label: '合同名称',
          fieldName: 'majorContractName',
          placeholder: '请选择',
          disabled: true,
          initialValue: '123',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '大客户项目名称',
          fieldName: 'majorProjectName',
          placeholder: '请选择',
          initialValue: '456',
          disabled: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '大客户名称',
          fieldName: 'majorCustomerName',
          placeholder: '请输入',
          initialValue: '789',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '大客户订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'DATE',
          label: '订单年月',
          fieldName: 'orderYearMonth',
          disabled: true,
          placeholder: '请输入',
          dateType: 'month',
          initialValue: moment().format('YYYY-MM')
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          disabled: true,
          itemList: [
            { text: '直销大客户订单', value: 60061002 },
            { text: '区域大客户订单', value: 60061003 },
            { text: '代交车大客户订单', value: 60061004 }
          ],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: p => {
            if (p) {
              this.formList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.formList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
              this.getNotPage(p);
            } else {
              this.formList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.formList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
            }
          }
        },
        {
          type: 'SELECT',
          label: '订单细分类型',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList: [],
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '运输方式',
          fieldName: 'transportType',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createDictList('6004'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否使用返利',
          fieldName: 'isRebate',
          placeholder: '请选择',
          disabled: true,
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '资金总余额(油)',
          fieldName: 'totalFuelAmount',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '资金总余额(电)',
          fieldName: 'totalEnergyAmount',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          precision: 2,
          label: '单车返利余额(电)',
          fieldName: 'singleRebateEnergyAmount',
          placeholder: '请选择',
          disabled: true
        },

        {
          type: 'INPUT_NUMBER',
          precision: 2,
          initialValue: 0.0,
          label: '建店补贴直接带出池余额(电)',
          fieldName: 'buildRebateEnergyAmountStart',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          initialValue: 0.0,
          label: '建店补贴单车带出池余额(电)',
          fieldName: 'buildRebateEnergyAmountEnd',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          precision: 2,
          label: '单车返利余额(油)',
          fieldName: 'singleRebateFuelAmount',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          initialValue: 0.0,
          label: '建店补贴直接带出池余额(油)',
          fieldName: 'buildRebateFuelAmountStart',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '建店补贴单车带出池余额(油)',
          fieldName: 'buildRebateFuelAmountEnd',
          placeholder: '请选择',
          precision: 2,
          initialValue: 0.0,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '单车返利使用金额(电)',
          fieldName: 'singleRebateEnergyTotalAmount',
          placeholder: '请选择',
          initialValue: 0.0,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '建店补贴直接带出池使用金额(电)',
          fieldName: 'buildRebateEnergyTotalAmountStart',
          placeholder: '请选择',
          initialValue: 0.0,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '建店补贴单车带出池使用金额(电)',
          fieldName: 'buildRebateEnergyTotalAmount',
          placeholder: '请选择',
          initialValue: 0.0,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '单车返利使用金额(油)',
          fieldName: 'singleRebateFuelTotalAmount',
          placeholder: '请选择',
          initialValue: 0.0,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '建店补贴直接带出池使用金额(油)',
          fieldName: 'buildRebateFuelTotalAmountStart',
          placeholder: '请选择',
          initialValue: 0.0,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '建店补贴单车带出池使用金额(油)',
          fieldName: 'buildRebateFuelTotalAmount',
          placeholder: '请选择',
          initialValue: 0.0,
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          precision: 2,
          label: '实付总金额',
          fieldName: 'realPayTotalAmount',
          disabled: true
        },
        {
          type: 'DATE',
          label: '要求交付日期',
          fieldName: 'requiredDeliveryDate',
          placeholder: '请选择',
          disabled: true,
          dateType: 'exactdate'
        },
        {
          type: 'SELECT',
          label: '开票类型',
          fieldName: 'invoiceType',
          placeholder: '请选择',
          itemList: this.createDictList('6044'),
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        // 2021-4-27 19:11:03  qinjh 泳哥让动力形式、资金类型 隐藏掉；
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          hidden: true,
          itemList: this.createDictList('6008'),
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: p => {
            if (p) {
              this.formList.map(x => {
                if (x.fieldName == 'accountType') {
                  x.initialValue = '';
                  x.itemList = [];
                }
              });
              this.gettmFiAccountType(p);
            } else {
              this.formList.map(x => {
                if (x.fieldName == 'accountType') {
                  x.initialValue = '';
                  x.itemList = [];
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountId',
          placeholder: '请选择',
          hidden: true,
          itemList: [],
          disabled: true,
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: p => {
            if (p) {
              this.getAmountDetail(this.dealerCode, p);
            }
          }
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remarks',
          hidden: false,
          disabled: true,
          placeholder: '请填写OA的审批号'
        },
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数量',
          dataIndex: 'orderNum',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '国补金额',
          dataIndex: 'subsidyAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣总金额',
          dataIndex: 'discountTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true,
          precision: 2,
          render: p => {
            return (
              <span class="discount" onClick={() => this.editHandle(p.row, 2)}>
                {p.row.discountTotalAmount}
              </span>
            );
          }
        },
        {
          title: '单车返利总金额',
          dataIndex: 'singleRebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true,
          precision: 2
        },
        {
          title: '建店返利类型',
          dataIndex: 'buildRebateType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          editable: false,
          filterItems: this.createDictList('6036').filter(x => x.value !== 60361001),
          editType: 'select',
          editItems: this.createDictList('6036').filter(x => x.value !== 60361001)
        },
        {
          title: '建店返利总金额',
          dataIndex: 'buildRebateTotalAmountLocalShow',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单车返利金额',
          dataIndex: 'singleRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          hidden: true
        },
        {
          title: '建店返利金额',
          dataIndex: 'buildRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          hidden: true
        },
        {
          title: '折后单价',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折前总金额',
          dataIndex: 'totalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true
        },
        {
          title: '折后总金额',
          dataIndex: 'afterTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          summation: true,
          precision: 2
        },
        {
          title: '收车地址',
          dataIndex: 'addressId',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            let str = '';
            p.row.addressList.map(x => {
              if (x.addressId == p.row.addressId) {
                str = x.detaildAddress;
              }
            });

            return <span>{str}</span>;
          }
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    createdTables() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '单据执行前状态',
          dataIndex: 'beforeOrderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6043'),
          render: p => {
            return <span>{this.createDictText(p.row.beforeOrderStatus, '6043')}</span>;
          }
        },
        {
          title: '单独执行后状态',
          dataIndex: 'afterOrderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6043'),
          render: p => {
            return <span>{this.createDictText(p.row.afterOrderStatus, '6043')}</span>;
          }
        },
        {
          title: '操作人员',
          dataIndex: 'createdName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '操作类型',
          dataIndex: 'operationType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.operationType}</span>;
          }
        },
        {
          title: '审批意见',
          dataIndex: 'auditOpinion',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 新增车辆
    addHandle() {
      this.$refs.from.SUBMIT_FORM();
      if (this.saveData) {
        let json = JSON.parse(JSON.stringify(this.saveData));
        this.carData = json;
        this.isaddCar = true;
      }
    },
    changeList(list) {
      this.singleRebateAmount = this.singleRebateTotalAmount;
      this.buildRebateAmount = this.buildRebateTotalAmount;
      this.tableList.map(x => {
        let single = x.singleRebateAmount * x.orderNum;
        let bulid = x.buildRebateAmount * x.orderNum;
        // let totalAmount=(x.singleRebate)*num+ (x.bulidRebate)*num;
        if (single >= this.singleRebateAmount) {
          x.singleRebateTotalAmount = this.singleRebateAmount;
          this.singleRebateAmount = 0;
        } else {
          x.singleRebateTotalAmount = single;
          this.singleRebateAmount = this.singleRebateAmount - single;
        }
        if (bulid >= this.buildRebateAmount) {
          x.buildRebateTotalAmount = this.buildRebateAmount;
          this.buildRebateAmount = 0;
        } else {
          x.buildRebateTotalAmount = bulid;
          this.buildRebateAmount = this.buildRebateAmount - bulid;
        }
      });
      // 计算单车返利总金额和建店返利增金额 实付总金额
      let singletotalAmount = 0;
      let buildtotalAmount = 0;
      let afterTotalAmount = 0;
      this.tableList.map(x => {
        singletotalAmount = x.singleRebateTotalAmount + singletotalAmount;
        buildtotalAmount = x.buildRebateTotalAmount + buildtotalAmount;
        afterTotalAmount = x.afterTotalAmount - x.singleRebateTotalAmount - x.buildRebateTotalAmount + afterTotalAmount;
      });
      this.formList.map(x => {
        if (x.fieldName == 'singleRebateTotalAmount') {
          x.initialValue = singletotalAmount;
        }
        if (x.fieldName == 'buildRebateTotalAmount') {
          x.initialValue = buildtotalAmount;
        }
        if (x.fieldName == 'realPayTotalAmount') {
          x.initialValue = afterTotalAmount;
        }
      });
    },
    seacherFun(type) {
      if (type == 1) {
        this.iscontract = true;
      } else if (type == 2) {
        this.isProject = true;
      } else {
        this.isowner = true;
      }
    },
    async getAmountDetail(code, accountTypeId) {
      let json = {};
      if (!accountTypeId) {
        json = { dealerCode: code };
      } else {
        json = { dealerCode: code, accountId: accountTypeId };
      }
      let res = await getAmount(json);

      for (let i in res.data) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res.data[i];
          }
        });
      }
      this.json = res.data;
      this.singleRebateTotalAmount = res.data.singleRebateAmount;
      this.buildRebateTotalAmount = res.data.buildRebateAmount;
      this.singleRebateAmount = res.data.singleRebateAmount;
      this.buildRebateAmount = res.data.buildRebateAmount;
      console.log(code, res.data, '余额池', this.singleRebateTotalAmount, this.buildRebateTotalAmount);
    },
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType, isAvailable: 10041001 });
      this.formList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else if (type == 2) {
        if (this.tableList.length < 1) {
          this.$notify({
            title: '警告',
            message: '请选择车辆',
            type: 'warning'
          });
          return;
        }
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          let arr = this.tableList;
          arr.map(x => {
            // x.powerType = 60081001;
            x.rebateTotalAmount = x.singleRebateTotalAmount + x.buildRebateTotalAmount;
          });
          json.orderId = this.rowData.orderId;
          json.detailDTOList = arr;
          json.totalAmount = this.json.totalAmount;
          json.singleRebateAmount = this.json.singleRebateAmount;
          json.buildRebateAmount = this.json.buildRebateAmount;
          json.majorContractNo = this.contractId;
          json.projectCode = this.ProjectId;
          json.saveType = 'DLR';
          json.orderStatus = 60431003;
          json.orderMonth = json.orderYearMonth.split('-')[1];
          json.orderYear = json.orderYearMonth.split('-')[0];
          let res = await saveOrder(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      } else {
        if (this.tableList.length < 1) {
          this.$notify({
            title: '警告',
            message: '请选择车辆',
            type: 'warning'
          });
          return;
        }
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          let arr = this.tableList;
          arr.map(x => {
            // x.powerType = 60081001;
            x.rebateTotalAmount = x.singleRebateTotalAmount + x.buildRebateTotalAmount;
          });
          json.orderId = this.rowData.orderId;
          json.detailDTOList = arr;
          json.majorContractNo = this.contractId;
          json.projectCode = this.ProjectId;
          json.totalAmount = this.json.totalAmount;
          json.singleRebateAmount = this.json.singleRebateAmount;
          json.buildRebateAmount = this.json.buildRebateAmount;
          json.orderMonth = json.orderYearMonth.split('-')[1];
          json.orderYear = json.orderYearMonth.split('-')[0];
          json.saveType = 'DLR';
          json.orderStatus = 60431001;
          let res = await saveOrder(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    },
    async carcloseHandler(val, carData) {
      this.isaddCar = false;
      if (val) {
        let is = false;
        this.tableList.map(x => {
          if (x.productCode == val.productCode) {
            is = true;
          }
        });
        if (!is) {
          // await this.getAddress(val)
          await this.getDisecount(val, carData);
        } else {
          // 提示
          this.$notify({
            title: '警告',
            message: '列表已有该车辆',
            type: 'warning'
          });
          return;
        }
      }
    },
    Discountclose() {
      this.isDiscount = false;
    },
    // 加载折扣信息
    async getDisecount(data, carData) {
      let res = await getDiscount({
        saveType: 'DLR',
        orderType: carData.orderType,
        orderDetailType: carData.orderDetailType,
        transportType: carData.transportType,
        isRebate: carData.isRebate,
        detailDTOList: [
          {
            singleAmount: data.basicPrice || 100000,
            powerType: 60081001,
            optionId: data.optionId,
            optionCode: data.optionCode
          }
        ]
      });

      res.data.discountTotalAmount = res.data.discountAmount * data.orderNum;
      res.data.totalAmount = res.data.singleAmount * data.orderNum;
      // 使用返利总金额
      // 车辆返利总额 与返利池余额比较
      // 如果大于返利池余额 则直接使用返利池余额
      // 返利池剩余0
      // 如果小于返利池余额 使用车辆返利总额
      // 返利池剩余为返利池剩余-车辆返利
      let single = res.data.singleRebate * data.orderNum;
      let bulid = res.data.bulidRebate * data.orderNum;
      console.log(single, bulid, this.singleRebateAmount, this.buildRebateAmount);
      // let totalAmount=(x.singleRebate)*num+ (x.bulidRebate)*num;
      if (single >= this.singleRebateAmount) {
        res.data.singleRebateTotalAmount = this.singleRebateAmount;
        this.singleRebateAmount = 0;
      } else {
        res.data.singleRebateTotalAmount = single;
        this.singleRebateAmount = this.singleRebateAmount - single;
      }
      if (bulid >= this.buildRebateAmount) {
        res.data.buildRebateTotalAmount = this.buildRebateAmount;
        this.buildRebateAmount = 0;
      } else {
        res.data.buildRebateTotalAmount = bulid;
        this.buildRebateAmount = this.buildRebateAmount - bulid;
      }

      res.data.afterTotalAmount = res.data.afterDiscountAmount * data.orderNum;
      let obj = (await getDealerAddress({ dealerCode: this.dealerCode, powerType: 60081001 })).data.map(x => ({ text: x.detaildAddress, value: x.addressId, addressProperty: x.addressProperty }));
      let json = Object.assign({}, data, carData, res.data);
      json.addressList = obj;
      json.buildRebateAmount = json.bulidRebate;
      json.singleRebateAmount = json.singleRebate;
      console.log(json, 'json');
      this.table.EXECUTE_INSERT([json]);
      // 计算单车返利总金额和建店返利增金额 实付总金额
      let singletotalAmount = 0;
      let buildtotalAmount = 0;
      let afterTotalAmount = 0;
      this.tableList.map(x => {
        singletotalAmount = x.singleRebateTotalAmount + singletotalAmount;
        buildtotalAmount = x.buildRebateTotalAmount + buildtotalAmount;
        afterTotalAmount = x.afterTotalAmount - x.singleRebateTotalAmount - x.buildRebateTotalAmount + afterTotalAmount;
      });
      this.formList.map(x => {
        if (x.fieldName == 'singleRebateTotalAmount') {
          x.initialValue = singletotalAmount;
        }
        if (x.fieldName == 'buildRebateTotalAmount') {
          x.initialValue = buildtotalAmount;
        }
        if (x.fieldName == 'realPayTotalAmount') {
          x.initialValue = afterTotalAmount;
        }
      });
    },
    changeNum(index, data) {
      console.log(index, data, '改变数量');
      let num = '';
      let str = '';
      for (let i in index) {
        num = index[i];
        console.log(i, 'i');
        str = i.split('|')[0];
      }
      let Rep = /^[1-9]\d*$/;
      console.log(num, Rep.test(num));
      if (Rep.test(num)) {
        this.singleRebateAmount = this.singleRebateTotalAmount;
        this.buildRebateAmount = this.buildRebateTotalAmount;
        this.tableList.map(x => {
          if (x._uid == str) {
            let single = x.singleRebateAmount * num;
            let bulid = x.buildRebateAmount * num;
            if (single >= this.singleRebateAmount) {
              x.singleRebateTotalAmount = this.singleRebateAmount;
              this.singleRebateAmount = 0;
            } else {
              x.singleRebateTotalAmount = single;
              this.singleRebateAmount = this.singleRebateAmount - single;
            }
            if (bulid >= this.buildRebateAmount) {
              x.buildRebateTotalAmount = this.buildRebateAmount;
              this.buildRebateAmount = 0;
            } else {
              x.buildRebateTotalAmount = bulid;
              this.buildRebateAmount = this.buildRebateAmount - bulid;
            }
          } else {
            let single = x.singleRebateAmount * x.orderNum;
            let bulid = x.buildRebateAmount * x.orderNum;
            if (single >= this.singleRebateAmount) {
              x.singleRebateTotalAmount = this.singleRebateAmount;
              this.singleRebateAmount = 0;
            } else {
              x.singleRebateTotalAmount = single;
              this.singleRebateAmount = this.singleRebateAmount - single;
            }
            if (bulid >= this.buildRebateAmount) {
              x.buildRebateTotalAmount = this.buildRebateAmount;
              this.buildRebateAmount = 0;
            } else {
              x.buildRebateTotalAmount = bulid;
              this.buildRebateAmount = this.buildRebateAmount - bulid;
            }
          }
        });
        data.discountTotalAmount = data.discountAmount * num;
        // 折前总金额 折后总金额
        data.totalAmount = data.singleAmount * num;
        data.afterTotalAmount = data.afterDiscountAmount * num;
        // 计算单车返利总金额和建店返利增金额 实付总金额
        let singletotalAmount = 0;
        let buildtotalAmount = 0;
        let afterTotalAmount = 0;
        this.tableList.map(x => {
          singletotalAmount = x.singleRebateTotalAmount + singletotalAmount;
          buildtotalAmount = x.buildRebateTotalAmount + buildtotalAmount;
          afterTotalAmount = x.afterTotalAmount - x.singleRebateTotalAmount - x.buildRebateTotalAmount + afterTotalAmount;
        });
        this.formList.map(x => {
          if (x.fieldName == 'singleRebateTotalAmount') {
            x.initialValue = singletotalAmount;
          }
          if (x.fieldName == 'buildRebateTotalAmount') {
            x.initialValue = buildtotalAmount;
          }
          if (x.fieldName == 'realPayTotalAmount') {
            x.initialValue = afterTotalAmount;
          }
        });
      }
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async editHandle(data, type) {
      if (type == 1) {
        await this.$confirm('是否确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            this.table.EXECUTE_DELETE([data]);
            console.log(this.tableList, '删除了');
            this.changeList(this.tableList);
          })
          .catch(() => {});
      } else {
        this.DiscountData = data;
        this.isDiscount = true;
      }
    },
    // 合同返回
    contractClose(val) {
      if (val) {
        this.formList.map(x => {
          if (x.fieldName == 'majorContractName') {
            x.initialValue = val.contractName;
            this.contractId = val.majorContractNo;
          }
        });
      }
      this.iscontract = false;
    },
    // 加载资金类型
    async gettmFiAccountType(powerType) {
      let res = (await gettmFiAccountType({ powerType: powerType, fundsType: 0, ownerCode: this.dealerCode })).data.map(x => ({ text: x.accountName, value: x.id }));
      this.formList.find(x => x.fieldName == 'accountId').itemList = res;
    },
    ProjectClose(val) {
      if (val) {
        this.formList.map(x => {
          if (x.fieldName == 'majorProjectName') {
            x.initialValue = val.contractName;
            this.ProjectId = val.majorContractNo;
          }
        });
      }
      this.isProject = false;
    },
    DiscountClose() {
      this.isDiscount = false;
    },
    // 获取详情
    async getDetail(data) {
      let res = await getDetail({ orderId: data.orderId });
      res.data.detailDTOList.map(x => {
        x.buildRebateTotalAmountLocalShow = ((x.buildRebateTotalAmountStart ? x.buildRebateTotalAmountStart : 0) + (x.buildRebateTotalAmount ? x.buildRebateTotalAmount : 0)).toFixed(2);
      });

      for (let i in res.data) {
        this.formList.map(x => {
          if (x.fieldName == 'buildRebateEnergyAmount') {
          } else if (x.fieldName == 'buildRebateFuelAmount') {
          } else if (x.fieldName == 'singleRebateEnergyAmount') {
          } else if (x.fieldName == 'singleRebateFuelAmount') {
          } else if (x.fieldName == 'totalEnergyAmount') {
          } else if (x.fieldName == 'totalFuelAmount') {
          } else if (x.fieldName == 'buildRebateEnergyAmountEnd') {
          } else if (x.fieldName == 'buildRebateEnergyAmountStart') {
          } else if (x.fieldName == 'buildRebateFuelAmountEnd') {
          } else if (x.fieldName == 'buildRebateFuelAmountStart') {
          } else if (i == x.fieldName) {
            x.initialValue = res.data[i];
          }
        });
      }
      this.dealerCode = res.data.dealerCode;
      this.getNotPage(res.data.orderType);
      this.getAmountDetail(res.data.dealerCode, res.data.accountType);
      this.gettmFiAccountType(res.data.powerType);

      this.tableList = res.data.detailDTOList || [];
    },
    async getLog(id) {
      let res = await getLog({ orderId: id });
      console.log(res, '2');
      this.tableLists = res.data || [];
    }
  }
};
</script>
